#!/bin/bash

client_file_path=$1
dbname=$2
dblogin=$3
dbpasswd=$4

function print_usage()
{
   echo "Usage: $0 <client file path> <dbname> <login> <passwd>"
}

if [ "x$dbpasswd" == "x" ]; then
   echo "`date +'%Y/%m/%d %H:%M:%S'` [ERROR] All arguments not available."
   print_usage
   exit 1
fi

lockfile="/tmp/importkeys2db.lock" 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file $lockfile..."
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting..." 
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file..."
`touch /tmp/importkeys2db.lock` 

let "total_processed_file_counter=0"
let "skipped_processed_file_counter=0"
let "error_processed_file_counter=0"
let "new_processed_file_counter=0"

tmpfile1=/tmp/importkeys.tmp.1
tmpfile2=/tmp/importkeys.tmp.2

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Listing files from $client_file_path... "

find >$tmpfile1 $client_file_path -ctime 0

C=`cat $tmpfile1 | wc -l`
if [ $C -eq 0 ]; then
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: No files found."
fi

cat $tmpfile1 | sed >$tmpfile2 s/\ [\ ]*/\+/g

for f in `cat $tmpfile2`
  do

  if [ $client_file_path != $f -a $client_file_path"out.zip" != $f -a $client_file_path".." != $f ]; then
      #echo "client file path $client_file_path"out.zip" and processing file $f are equal"
 
      f_basename=`basename $f`
      f=`echo $f | sed s/\+/\ /g`
      f_basename=`echo $f_basename | sed s/\+/\ /g`
      echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing file $f_basename... "
      
      #echo "file: $f_basename"
      let "total_processed_file_counter=$total_processed_file_counter+1"  
      /data/scripts/ImportKeys2DB.pl --log /data/home/simgen/ImportKeys2DB.log "$f" $dbname $dblogin $dbpasswd
      
      res=$?
      
      # res=0  -> process ok
      # res=1  -> function intro failed
      # res=2  -> function parse args failed
      # res=3  -> db connect failed
      # res=4  -> slsecd connect failed
      # res=5  -> lock file exists
      # res=6  -> lock file create failed
      # res=7  -> client file format not supported
      # res=8  -> client file already processed
      # res=9  -> event_id retrieve failed
      # res=10 -> event_id not expected value
      # res=11 -> data encipher failed
      # res=12 -> secure data update failed
      # res=13 -> function main failed
      # res=14 -> event insert failed when opl unknown
  
      if [ $res -eq 5 -o $res -eq 4 -o $res -eq 3 -o $res -eq 6 -o $res -eq 1 -o $res -eq 2 -o $res -eq 13 ]; then
	  let "error_processed_file_counter=$error_processed_file_counter+1"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ERROR CODE=$res"
	  break;
	  
      elif [ $res -eq 8 ]; then
	  let "skipped_processed_file_counter=$skipped_processed_file_counter+1"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: File alredy proccessed. Skipping...."
	  
      elif [ $res -eq 7 -o $res -eq 9 -o $res -eq 10 -o $res -eq 11 -o $res -eq 12 -o $res -eq 14 ]; then
	  let "error_processed_file_counter=$error_processed_file_counter+1"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ERROR CODE=$res"
	  
      elif [ $res -eq 0 ]; then
	  let "new_processed_file_counter=$new_processed_file_counter+1"
	  echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Done."
	  
      else
	  let "error_processed_file_counter=$error_processed_file_counter+1"
	  
      fi
  fi
done

#rm -f $tmpfile1 $tmpfile2
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of Process."
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ### Summary: #################################################"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Total file Processed: $total_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:     Included skipped: $skipped_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:       Included error: $error_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT:         Included new: $new_processed_file_counter"
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: ##################################"

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file..."
`rm /tmp/importkeys2db.lock` 

#error=`grep "\[ERROR\]" /tmp/importkeys2db.sh.log`
#res=$?
#if [ $res -eq 0 ]; then
    /data/scripts/sendmail.pl -f multisimgen.support@tcl.com -t raffi.semerciyan@jrdcom.com -s "Multisimgen >ImportKeys2DB.sh< notification" /tmp/importkeys2db.sh.log
#fi
